/**
 * 菜单数据
 */
import {  NIcon } from 'naive-ui'
import { h } from 'vue';
export { iconOptions } from "../../../icon";

export const baseForm = () => ({
    id: null,
    type: 1101, //类型
    label: '',// 名称
    name: '', // 路由名称
    parentId: 0, // 父id
    perm: '', // 权限标识
    hidden: 1102,
    icon: '',
    sort: 0,
    apiIds: []
})



export const renderSelectIcon = ({ option = { icon: '', label: '' } }) => {
    return h(
        'div',
        {
            style: {
                display: 'flex',
                alignItems: 'center'
            }
        },
        [
            h(NIcon, {
                style: {
                    marginRight: '12px'
                }
            },{ default: () => h(option.icon) }),
            option.label
        ]
    )
}

export const renderLabel = (option:any) => {
    return h(
        'div',
        {
            style: {
                display: 'flex',
                alignItems: 'center'
            }
        },
        [
            h(NIcon, {
                style: {
                    marginRight: '12px'
                }
            },{ default: () => h(option.icon) }),
            option.label
        ]
    )
}

// 菜单类型
export const menuType = [
    { name: '目录', value: 1101 },
    { name: '菜单', value: 1102 },
    { name: '按钮功能', value: 1103 }
]
// 表单校验
export const formRules = {
    name: {
        required: true,
        message: '请输入标签名称'
    },
    label: {
        required: true,
        message: '请输入路由名称'
    },
    parentId:{
        required: true,
        message: '请选择父目录'
    },
    perm: {
        required: true,
        message: '请输入权限标识，例：system-menu-xx'
    }
}

interface MenuList {
    key: number,
    label: string,
    path: string,
    children: [] | null
}

// 格式化方法菜单
export const formatMenu = (menuList: any[] = []) => {
    const formatTree = (allMenu: any[], parentId: number): any => {
        return allMenu.filter(menuItem => menuItem.parentId == parentId).map(item => {
            let children = formatTree(allMenu.filter(menuItem => menuItem.parentId !== parentId), item.id);
            return {
                key: item.id,
                label: item.label,
                name: item.name,
                path: item.url,
                parentId: item.parentId || 0,
                children: children.length > 0 ? children : null // 适应ui框架,空的时候如果为空数组，会有箭头
            }
        })
    }
    return formatTree(menuList, 0);
}

export function getDeleteKey(menuKey: number | string, menus: any[]) {
    function getMenuByKey(menuKey: number | string, menus: any[]) {
        for (let i = 0; i < menus.length; i++) {
            const item = menus[i];
            if (item.key == menuKey) {
                deleteMenu = item;
                return;
            }
            if (item.children && item.children.length > 0) {
                getMenuByKey(menuKey, item.children);
            }
        }
    }

    function getChildrenKey(menus: any[]) {
        menus.forEach(item => {
            menuKeys.push(item.key);
            if (item.children && item.children.length > 0) {
                getChildrenKey(item.children)
            }
        })
    }

    let menuKeys = [];
    menuKeys.push(menuKey);
    let deleteMenu = { children: [] };
    getMenuByKey(menuKey, menus);
    if (deleteMenu && deleteMenu.children) {
        getChildrenKey(deleteMenu.children);
    }
    return menuKeys;
}


// 约束接口
export interface MenuType {
    type: number, //类型
    name: string, // 名称
    parentId: number, // 父id
    perm: string, // 权限标识
    icon?: string,
    sort: number
}

export interface TreeDataType {
    label: any
    key: string
    children: [] | null
}

export interface BaseFormType {
    id: any,
    type: number, //类型
    label: string,// 名称
    name?: string, // 路由名称
    parentId: number, // 父id
    perm?: string, // 权限标识
    hidden: number, // 是否隐藏 1101隐藏 1102不隐藏
    icon?: string,
    sort: number,
    apiIds: Array<number | string>
}
